/*
 * $Id: ShemSimpleTextReader.java,v 1.3 2009/07/10 20:02:16 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.shemjob.reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.ge.healthcare.autosc.processor.exception.ShemReaderException;
import com.ge.healthcare.autosc.processor.magmon.IMagnetMonitorProcessor;
import com.ge.healthcare.autosc.processor.magmon.MagnetMonitorUtility;

/**
 * A simple SHeM text reader built using a BufferedReader.
 * 
 * @author 212042946
 * 
 */
public class ShemSimpleTextReader implements ShemTextReader {

	/**
	 * Name used to identifier the reader.
	 */
	private String theReaderId = null;

	/**
	 * The buffered reader which does all the work.
	 */
	private BufferedReader baseReader = null;

	/**
	 * Create a simple SHeM text reader.
	 */
	public ShemSimpleTextReader() {
		super();
	}

	/**
	 * Open the text reader.
	 * 
	 * @param readerId
	 *            Name used to identifier the reader.
	 * 
	 * @param rawSource
	 *            The raw input stream for reading the data. It is the
	 *            responsibility of the caller to make sure that this is
	 *            buffered.
	 * 
	 * @param helper
	 *            Object providing the utility objects and methods.
	 * 
	 * @param utility
	 *            Utility class with magnet monitor methods.
	 * 
	 * @param magmon
	 *            The magnet monitor, which is used to get shem type and
	 *            parameter configuration information for configurable readers.
	 */
	public void open(String readerId, InputStream rawSource,
			MagnetMonitorUtility utility,
			IMagnetMonitorProcessor magmonProcessor) {
		theReaderId = readerId;
		baseReader = new BufferedReader(new InputStreamReader(rawSource));
	}

	/**
	 * Close the reader.
	 * 
	 * @exception AscServiceAbortException
	 *                If an error is detected closing the reader, an error is
	 *                logged and then an abort exception is thrown.
	 */
	public void close() throws ShemReaderException {
		if (baseReader == null) {
			return;
		}
		try {
			baseReader.close();
		} catch (IOException e) {
			throw new ShemReaderException(e);
		} finally {
			baseReader = null;
		}
	}

	/**
	 * Silently close the reader. This never throws an exception. If an I/O
	 * error is detected closing a stream, it is not logged. Other errors may be
	 * logged at the discretion of the implementor.
	 */
	public void silentlyClose() {
		if (baseReader != null) {
			try {
				baseReader.close();
			} catch (IOException e) {
			} finally {
				baseReader = null;
			}
		}
	}

	/**
	 * Read a line of text. If the text is read from an underlying reader, a
	 * line is considered to be terminated by any one of a line feed, a carriage
	 * return, or a carriage return followed immediately by a linefeed. Text
	 * lines can also be constructed by the reader, in which case the reader
	 * defines a text line. Typically, a text line is a record from an
	 * underlying data source.
	 * 
	 * @return A string containing the contents of the line, not including any
	 *         line-termination characters, or null if the end of the data has
	 *         been reached.
	 * 
	 * @exception AscServiceAbortException
	 *                If an error is detected reading a line of text, an error
	 *                is logged and then an abort exception is thrown.
	 */
	public String readLine() throws ShemReaderException {
		String line = null;
		if (baseReader != null) {
			try {
				line = baseReader.readLine();
			} catch (IOException e) {
				throw new ShemReaderException("Unable to read a line from "
						+ theReaderId, e);
			}
		} else {
			throw new ShemReaderException("Record reader not open");
		}
		return line;
	}
}
